package ru.mera.aaclog.parser;

import ru.mera.aaclog.db.MessageHeader;

/**
 * @version 1.0 02/03/2013
 * @version 1.2 23/03/2013
 * @author anavdeev
 */
public class MessageParser {

    public final static String SIP_MESSAGE_TRACE_TAG = "SIP Message Trace :";
    private final static String TRANS_TAG = "Transaction:";
    private final static String FROM_TAG = "From:";
    private final static String TO_TAG = "To:";
    private final static String CSEQ_TAG = "CSeq:";
    private final static String CALLID_TAG = "Call-ID:";
    private final static String LEN_TAG = "Content-Length:";
    private final static String TYPE_TAG = "Content-Type:";
    private final static String CONTACT_TAG = "Contact:";
    private final static String USERAGENT_TAG = "User-Agent:";
    private final static String USERAGENT_TAG2 = "server:";
    
    private final static String TAG_FIELD = "tag";
    
    private String rawPackage;
    private String xmlContext;

    public MessageHeader parse(String rawPackage) {
        setRawPackage(rawPackage);
        MessageHeader messageHeader = new MessageHeader();

        String value = parseTag(SIP_MESSAGE_TRACE_TAG);
        if (value != null && !value.isEmpty()) {
            try {
                messageHeader.setSipMessageType(value.split(":")[1].trim());
            } catch (Exception e) {
                messageHeader.setSipMessageType(value);
            }
        }
        value = parseTag(TRANS_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setTrans(value.split(TRANS_TAG)[1].trim());
        }
        value = parseTag(FROM_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setFrom(value.split(FROM_TAG)[1].trim());
            value = parseField(messageHeader.getFrom(), TAG_FIELD);
            if (value != null && !value.isEmpty()) {
                messageHeader.setFromTag(value);
            }
        }
        value = parseTag(TO_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setTo(value.split(TO_TAG)[1].trim());
            value = parseField(messageHeader.getTo(), TAG_FIELD);
            if (value != null && !value.isEmpty()) {
                messageHeader.setToTag(value);
            }
        }
        value = parseTag(CSEQ_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setcSeq(value.split(CSEQ_TAG)[1].trim());
        }
        value = parseTag(CALLID_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setCallId(value.split(CALLID_TAG)[1].trim());
        }
        value = parseTag(LEN_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setLen(value.split(LEN_TAG)[1].trim());
        }
        value = parseTag(TYPE_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setType(value.split(TYPE_TAG)[1].trim());
        }
        value = parseTag(CONTACT_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setContact(value.split(CONTACT_TAG)[1].trim());
        }
        value = parseTag(USERAGENT_TAG);
        if (value != null && !value.isEmpty()) {
            messageHeader.setUserAgent(value.split(USERAGENT_TAG)[1].trim());
        } else {
            value = parseTag(USERAGENT_TAG2);
            if (value != null && !value.isEmpty()) {
                messageHeader.setUserAgent(value.split(USERAGENT_TAG2)[1].trim());
            }
        }
        setXmlContext(parseXML());
        return messageHeader;
    }

    private String parseTag(String tag) {
        String value = null;
        int index = getRawPackage().indexOf(tag);
        if (index != -1) {
            int endIndex = getRawPackage().indexOf("\n", index);
            if (endIndex != -1) {
                value = getRawPackage().substring(index, endIndex).replace('\r', ' ');
            }
        }
        return value;
    }
    
    private String parseField(String tagValue, String fieldName) {
        String value = null;
        String[] params = tagValue.split(";");
        for (String param : params) {
            try {
                String[] rawValue = param.split("=");
                if (rawValue[0].equalsIgnoreCase(fieldName)) {
                    value = rawValue[1];
                    break;
                }
            } catch (Exception e) {
                continue;
            }
        }
        return value;
    }

    private String parseXML() {
        String value = parseTag("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        if (value != null && !value.isEmpty()) {
            int index = value.indexOf(">");
            // add '\n'
            if (index != -1 && index + 1 < value.length()) {
                value = value.replaceAll("><", ">\n<");
            }
        } else {
            value = "";
        }
        return value;
    }

    public String getRawPackage() {
        return rawPackage;
    }

    public void setRawPackage(String rawPackage) {
        this.rawPackage = rawPackage;
    }

    public void setXmlContext(String xmlContext) {
        this.xmlContext = xmlContext;
    }

    public String getXmlContext() {
        return xmlContext;
    }

}
